package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.componet.SelectData;
import cn.acewill.yxd.yunxundian.model.Schedule;
import cn.acewill.yxd.yunxundian.model.Shop;

/**
 * Created by XXX on 2017/5/2.
 */

public class CheckTaskAdapter extends BaseAdapter{
    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());
    private Context context;
    private List<Schedule> list;
    private SetStartTime setStartTime;

    public CheckTaskAdapter(Context context,List<Schedule> list,SetStartTime setStartTime){
        this.context=context;
        this.list=list;
        this.setStartTime=setStartTime;
    }
    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.schedule_check_task,parent,false);
            holder=new ViewHolder();
            holder.scheduleTime= (TextView) convertView.findViewById(R.id.scheduleTime);
            holder.taskName= (TextView) convertView.findViewById(R.id.taskName);
            holder.shopName= (TextView) convertView.findViewById(R.id.shopName);
            holder.mustTask= (ImageView) convertView.findViewById(R.id.mustTask);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Schedule bean=list.get(position);
        String array=bean.getSubtitle();
//       List<Subtitle> sublist=JSON.parseArray(array,Subtitle.class);
        try {
            Shop shop=db.selector(Shop.class).where("ID","=",bean.getShopID()).findFirst();
            holder.taskName.setText(bean.getTitle());
            holder.shopName.setText(shop.getShopName());
        } catch (DbException e) {
            e.printStackTrace();
        }
        holder.scheduleTime.setText(bean.getSchStartTime());
        if (bean.getIsAssign()==1){
            holder.mustTask.setVisibility(View.VISIBLE);
        }else{
            holder.mustTask.setVisibility(View.INVISIBLE);
        }
        if(bean.getSchStartTime()==null){

        }else{
            holder.scheduleTime.setText(bean.getSchStartTime());
        }
        holder.scheduleTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectData data=new SelectData(context,true);
                data.showAtLocation(holder.scheduleTime, Gravity.BOTTOM,0,0);
                data.setDateClickListener(new SelectData.OnDateClickListener() {
                    @Override
                    public void onClick(String year, String month, String day, String hour, String minute) {
                        String str=year+"-"+month+"-"+day+" "+hour+":"+minute;
                        holder.scheduleTime.setText(str);
                        setStartTime.onTimeSet(str,position);
                    }
                });
            }
        });

        return convertView;
    }
    class ViewHolder{
        TextView taskName;
        TextView shopName;
        TextView scheduleTime;
        ImageView mustTask;
    }

    public interface SetStartTime{
        void onTimeSet(String str,int position);
    }
}
